package com.dayaokeji.rhythmschoolstudent.client.home.course.services;

import com.dayaokeji.rhythmschoolstudent.utils.ServerCallback;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.ServerResponse;
import com.dayaokeji.server_api.api.CourseScoringApi;
import com.dayaokeji.server_api.domain.CourseScoringRank;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;

/**
 * ********************************************************************
 * *  Created by Fei wo on 2018-12-3
 * ********************************************************************
 * *  version 1.0
 * ********************************************************************
 * *  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 * ********************************************************************
 * * Activity里面业务太多，逐渐抽离成服务
 * ********************************************************************
 */
public class CourseScoringService {

    private static CourseScoringService instance;

    private CourseScoringService(){}

    public static CourseScoringService getInstance() {
        if (null == instance) {
            instance = new CourseScoringService();
        }
        return instance;
    }

    /**
     * 获取排名和得分
     * @return
     */
    public Observable<CourseScoringRank> findCourseScoringRank(Integer courseDetailId,String uid) {
        return Observable.create(emitter -> {
            ServerCallback<List<CourseScoringRank>> callback = callback(emitter);
            callback.setShowNetworkError(false); // close network error toast
            ApiUtils.getApi(CourseScoringApi.class).findCourseRankingByUid(courseDetailId,uid).enqueue(new ServerCallback<List<CourseScoringRank>>() {
                @Override
                public void onResponse(boolean success, ServerResponse<List<CourseScoringRank>> response) {
                    if (success) {
                        if (response.getBody().isEmpty()) {
                            emitter.onNext(new CourseScoringRank());
                        }else{
                            emitter.onNext(response.getBody().get(0));
                        }
                    }
                }
            });
        });
    }

    /**
     * req callback
     * @param emitter
     * @return
     */
    private ServerCallback<List<CourseScoringRank>> callback(ObservableEmitter<CourseScoringRank> emitter){
        return new ServerCallback<List<CourseScoringRank>>() {
            @Override
            public void onResponse(boolean success, ServerResponse<List<CourseScoringRank>> response) {
                if (success) {
                    if (response.getBody().isEmpty()) emitter.onNext(null);
                    emitter.onNext(response.getBody().get(0));
                }
            }
        };
    }

    /**
     * find course rank list
     * @param courseDetailId
     * @return
     */
    public Observable<List<CourseScoringRank>> findCourseScoringRanks(Integer courseDetailId) {
        return Observable.create(emitter -> {
            final HashMap<String,Object> params = new HashMap<>();
            params.put("detailId", courseDetailId);
            ApiUtils.getApi(CourseScoringApi.class).findCourseRankingByUid(courseDetailId,"").enqueue(new ServerCallback<List<CourseScoringRank>>() {
                @Override
                public void onResponse(boolean success, ServerResponse<List<CourseScoringRank>> response) {
                        emitter.onNext(response.getBody());
                }
            });
        });
    }
}
